package main.adapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.hichip.campro.R;
import com.hichip.control.HiCamera;
import java.io.File;

import activity.Left.LeftGetDataModel;
import activity.Left.LeftLocalVideoPhotoActivity;
import activity.LocalFile.LocalFileActivity2;
import activity.setting.SettingActivity;
import activity.video.RemoteRecordingListActivity;
import bean.LeftLocalFileBean;
import bean.MyCamera;
import common.HiDataValue;
import utils.BitmapUtils;
import utils.HiToast;
import utils.HiTools;
/**
 * Created by lt on 2017\12\16 0016.
 */

public class CameraListSmallAdapter extends BaseAdapter {
    Context mContext;
    private LayoutInflater mInflater;


    public CameraListSmallAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return HiDataValue.CameraList.size();
    }

    @Override
    public Object getItem(int position) {
        return HiDataValue.CameraList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyCamera camera = HiDataValue.CameraList.get(position);
        if (camera == null) {
            return null;
        }
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_main_camera_small, null);
            holder.iv_snapshot = convertView.findViewById(R.id.iv_snapshot);
            holder.txt_nikename = convertView.findViewById(R.id.nickname_camera_item);
            holder.txt_uid = convertView.findViewById(R.id.uid_camera_item);
            holder.tv_small_camera_status = convertView.findViewById(R.id.tv_small_camera_status);
            holder.iv_video=convertView.findViewById(R.id.iv_video);
            holder.iv_photo=convertView.findViewById(R.id.iv_photo);
            holder.item_setting=convertView.findViewById(R.id.item_setting);
            holder.iv_small_status=convertView.findViewById(R.id.iv_small_status);
            holder.iv_small_bell=convertView.findViewById(R.id.iv_small_bell);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (camera.getAlarmState() == 0) {
            holder.iv_small_bell.setVisibility(View.GONE);
        } else {
            holder.iv_small_bell.setVisibility(View.VISIBLE);
        }
        if (holder != null) {
            if (camera.snapshot == null) {
                holder.iv_snapshot.setImageResource(R.mipmap.videoclip);
            } else {
                Bitmap bitmap = BitmapUtils.setRoundedCorner(camera.snapshot, HiTools.dip2px(mContext,6));
                holder.iv_snapshot.setImageBitmap(bitmap);
            }

            holder.txt_nikename.setText(camera.getNikeName());
            holder.txt_uid.setText(camera.getUid());

            //设置摄像机状态
            HiTools.setCameraStatusBack(mContext,holder.iv_small_status,holder.tv_small_camera_status,camera.getConnectState());
            if (camera.isSystemState == 1 && camera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
                holder.tv_small_camera_status.setText(mContext.getString(R.string.tips_restart));
            }
            if (camera.isSystemState == 2 && camera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
                holder.tv_small_camera_status.setText(mContext.getString(R.string.tips_recovery));
            }
            if (camera.isSystemState == 3 && camera.getConnectState() == HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
                holder.tv_small_camera_status.setText(mContext.getString(R.string.tips_update));
            }
            // 录像回放
            holder.iv_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (camera != null&&camera.getConnectState()==HiCamera.CAMERA_CONNECTION_STATE_LOGIN) {
                        //Intent intent = new Intent(mContext, VideoActivity.class);
                        Intent intent=new Intent(mContext, RemoteRecordingListActivity.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID, camera.getUid());
                        mContext.startActivity(intent);
                        if (mContext != null && (mContext instanceof Activity)) {
                            Activity act = (Activity) mContext;
                            act.overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
                        }
                    }else {
                        HiToast.showToast(mContext,"当前设备不在线!");
                    }
                }
            });

            //该摄像机 本地文件
            holder.iv_photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(camera!=null){
                        Intent intent=new Intent(mContext, LocalFileActivity2.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID,camera.getUid());
                        mContext.startActivity(intent);
                        if (mContext != null && (mContext instanceof Activity)) {
                            Activity act = (Activity) mContext;
                            act.overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
                        }
                    }
                }
            });
            //设置
            ViewHolder finalHolder = holder;
            holder.item_setting.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onClick(View v) {
                    if (camera != null) {
                        Intent intent = new Intent(mContext, SettingActivity.class);
                        intent.putExtra(HiDataValue.EXTRAS_KEY_UID, camera.getUid());
                        if (mContext != null && (mContext instanceof Activity)) {
                            Activity act = (Activity) mContext;
                            //转场动画设置
                            ActivityOptions options=ActivityOptions.makeSceneTransitionAnimation(act, finalHolder.iv_snapshot,"iv");
                            mContext.startActivity(intent,options.toBundle());
                        }
                    }
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        public ImageView iv_snapshot;
        public TextView txt_nikename;
        public TextView txt_uid;
        public TextView tv_small_camera_status;
        public ImageView iv_video;
        public ImageView iv_photo;
        public ImageView item_setting;
        public ImageView iv_small_status;
        public ImageView iv_small_bell;
    }

    // 获取图片数量，
    public int getFileCount(String uid) {
        String path = HiDataValue.getPathCameraSnapshot(uid,mContext);
        File folder = new File(path);
        String[] imageFiles = folder.list();
        if (imageFiles == null) {
            return 0;
        }
        int sum = 0;
        sum = imageFiles.length;
        return sum;
    }
}
